#include <stdio.h>

void get_nextval(int *nextval, char const *p) {
    int j = 1, k = 0;
    nextval[1] = 0;
    while (j < 8) {
        if (k == 0 || p[j] == p[k])
            if (p[++j] != p[++k])
                nextval[j] = k;
            else
                nextval[j] = nextval[k];
        else
            k = nextval[k];
    }
}

int main() {
    int nextval[9];
    char *p = "abaabcac";
    get_nextval(nextval, p);
    for (int i = 1; i < 9; i++)
    {
        printf("%d ", nextval[i]);
    }
    return 0;
}